import re
from typing import Callable

from rest_framework.serializers import ValidationError


def regexp_validator(regexp: str) -> Callable[[str], None]:
    pattern = re.compile(regexp)

    def validate(value: str) -> None:
        if not pattern.fullmatch(value):
            raise ValidationError(f'{value} does not match pattern: {regexp}')

    return validate
